import 'package:flutter/material.dart';
import 'package:hx_im_plugin_example/hq_im_ui/utils/envent_bus.dart';
import 'package:hx_im_plugin_example/hq_im_ui/widgets/hq_common_widgets.dart';

/// 录制语音动画

class HqChatVoiceRecordAnimated extends StatefulWidget {

  final bool isVoiceState;
  HqChatVoiceRecordAnimated(
      [
        this.isVoiceState = true
      ]);

  @override
  _HqChatVoiceRecordAnimatedState createState() => _HqChatVoiceRecordAnimatedState();
}

class _HqChatVoiceRecordAnimatedState extends State<HqChatVoiceRecordAnimated> with SingleTickerProviderStateMixin {

  Animation<double> animation;
  AnimationController controller;
  bool _isVoiceState;

  initState() {
    super.initState();

    controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );

    /// 图片宽高从0变到300
    animation = Tween(begin: 0.0, end: 6.0).animate(controller);

    _isVoiceState = widget.isVoiceState;
    _initEvents();
  }

  dispose() {
    /// 路由销毁时需要释放动画资源
    controller.dispose();
    super.dispose();
  }

  _initEvents() {
    bus.on('voiceAnimated', (arg) {
      _isVoiceState = arg["isVoiceState"];
      recordStratStopAnimated(arg);
    });
  }

  /// 启动/停止动画
  void recordStratStopAnimated(Map arg) {
    if (arg["isPlaying"]) {
      controller.repeat();
    } else {
      controller.stop();
      controller.reverse(from: 0);
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {

    return Offstage(
      offstage: _isVoiceState,
      child: Center(
        child: Opacity(
          opacity: 0.5,
          child: Container(
            width: 160,
            height: 160,
            decoration: BoxDecoration(
              color: Color(0xff77797A),
              borderRadius: BorderRadius.all(Radius.circular(20.0)),
            ),
            child: Container(
              margin: EdgeInsets.only(
                top: sHeight(10),
              ),
              width: sWidth(100),
              height: sHeight(110),
              child: HqVoiceRecordAnimatedImage(
                animation: animation,
              ),
            ),
          ),
        ),
      ),
    );
  }
}



class HqVoiceRecordAnimatedImage extends AnimatedWidget {
  HqVoiceRecordAnimatedImage({Key key, Animation<double> animation})
      : super(key: key, listenable: animation);

  Widget build(BuildContext context) {
    final Animation<double> animation = listenable;

    String imgName = 'images/voice_volume_1.png';
    switch (animation.value.toInt()) {
      case 0:
        imgName = 'images/voice_volume_1.png';
        break;
      case 1:
        imgName = 'images/voice_volume_2.png';
        break;
      case 2:
        imgName = 'images/voice_volume_3.png';
        break;
      case 3:
        imgName = 'images/voice_volume_4.png';
        break;
      case 4:
        imgName = 'images/voice_volume_5.png';
        break;
      case 5:
        imgName = 'images/voice_volume_6.png';
        break;
      case 6:
        imgName = 'images/voice_volume_7.png';
        break;
      default:
    }
    return new Center(
      child: Image.asset(imgName),
    );
  }
}